<template>
  <div class="app-container table-app-container">
    <div class="fixed-table">
      <div class="fixed-table-actionbar">
        <TableFilter />
        <ActionBar v-if="!forSelection" v-role="['zongjian']" />
      </div>
      <PageableTable
        :loading="loading"
        :data="insideTableData"
        :total="totalElements"
        :page="currentPage"
        :page-size="pageSize"
        :table-height="tableHeight"
        class="fixed-table-table"
        @page-change="onPageChange"
        @sort-change="onSortChange"
      >
        <el-table-column v-for="column in insideColumns" :key="column.key" :label="column.title" :width="column.width" :prop="column.key" :sortable="column.sortable">
          <template slot-scope="scope">
            <column-render :render="column.render" :row="scope.row" :column="column" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button title="编辑" type="text" size="mini" icon="el-icon-edit" @click="edit(row.id)" />
            <el-button v-if="!row.releaseStatus" title="删除" type="text" size="mini" icon="el-icon-delete" @click="remove(row.id)" />
            <el-button v-if="!row.releaseStatus" title="发布" type="text" size="mini" icon="el-icon-s-promotion" @click="onRelease(row.id,row.releaseStatus)" />
          </template>
        </el-table-column>
      </PageableTable>
    </div>
    <FormDialog class="fixed-dialog-height" title="编辑" :show.sync="dialogFormVisible" width="90%" @on-ok="ok" @on-cancel="cancel">
      <EditForm v-if="dialogFormVisible" :id="editId" ref="editForm" />
    </FormDialog>
  </div>
</template>

<script>
// secondary package based on el-pagination
// import SelectDialog from '@neu/biz-core/components/SelectDialog'
import FormDialog from '@neu/biz-core/components/FormDialog'
// 表格查询组件独立编写，需要传递pageable-table实例的mixin方法filter
import TableFilter from './components/tableFilter'
// 编辑实例组件独立编写，需要传递id属性，判断新建还是更新操作
import EditForm from './components/editForm'
import ActionBar from './components/actionBar'
import PageableTable from '@neu/biz-core/components/PageableTable'
import databind from '@neu/biz-core/mixins/databind'
import { formatDate } from '@neu/biz-core/utils/utils'
import { page, remove, release } from '@neu/dcom-ui/api/insuranceRegister'

export default {
  name: 'RegisterTable',
  components: {
    TableFilter,
    FormDialog,
    EditForm,
    ActionBar,
    PageableTable
  },
  // 加入混入，使表格混入更多的api操作
  mixins: [databind],
  data() {
    return {
      r: page,
      d: remove,
      insideColumns: [
        { title: '服务编号', key: 'code', width: '150' },
        { title: '服务厅局', key: 'registerUnit', width: '200',
          render: (h, params) => {
            if (params.row.registerUnit) {
              return (
                <el-row>
                  { params.row.registerUnit.map(e => {
                    return (
                      <el-col>
                        <el-tag type='primary' size='small'> { e.unitName } </el-tag>
                      </el-col>
                    )
                  }) }
                </el-row>
              )
            }
          }
        },
        { title: '服务开始时间', key: 'startTime',
          render: (h, params) => { return (<span>{ formatDate(params.row.startTime, 'YYYY-MM-DD') }</span>) }
        },
        { title: '服务开始时间', key: 'endTime',
          render: (h, params) => { return (<span>{ formatDate(params.row.endTime, 'YYYY-MM-DD') }</span>) }
        },
        { title: '备注信息', key: 'remarks', width: '200' },
        { title: '客户联系人', key: 'customerContact' },
        { title: '客户联系电话', key: 'customerContactPhone' },
        { title: '发布状态', key: 'releaseStatus',
          render: (h, params) => {
            const text = params.row.releaseStatus ? '已发布' : '未发布'
            if (text === '已发布') {
              return (
                <el-tag type='success' size='small'> { text } </el-tag>
              )
            } else {
              return (
                <el-tag size='small'> { text } </el-tag>
              )
            }
          }
        }
      ],
      dialogFormVisible: false,
      editId: -1
    }
  },
  methods: {
    // 表格编辑按钮点击事件处理
    edit(id) {
      // 打开对话框
      this.editId = id
      this.dialogFormVisible = true
      // 更新当前被编辑实体的ID，自动传递至编辑表单
    },
    // 编辑资产对话框确认事件处理
    ok() {
      // 直接调用表单实例的保存方法
      this.$refs.editForm.save().then(response => {
        if (response === false) return
        this.$message({
          showClose: true,
          message: '操作成功！',
          type: 'success'
        })
        // 调用pageable-table实例的mixin方法refresh()，更新表格
        this.refresh()
        // 关闭对话框
        this.editId = -1
        this.dialogFormVisible = false
      })
    },
    cancel() {
      this.editId = -1
      this.$refs.editForm.reset()
      // 关闭对话框
      this.dialogFormVisible = false
    },
    onRelease(id, status) {
      this.$confirm(status === true ? '确定取消发布?' : '确定发布?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        release(id, !status).then(response => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.refresh()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>
